package com.weipt.oper;

/**
 * @program: Learning
 * @description:  利用sleep，模拟网络延迟.sleep结束后，不会释放锁
 * @Company: 汇华(北京)信息咨询服务有限公司
 * @author: peter.wei
 * @create: 2020-07-25 10:17
 **/
public class Sleep2 {
    static class MyRunnable implements Runnable{
        private int ticket = 20;
        @Override
        public void run() {
            while (true){
                    if(ticket<=0){
                        break;
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"--->抢到了第："+ticket+"张票");
                    ticket--;
                }

        }
    }

    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread t = new Thread(myRunnable, "小明");
        Thread t2 = new Thread(myRunnable, "大黑");
        Thread t3 = new Thread(myRunnable, "张红");
        t.start();
        t2.start();
        t3.start();
    }
}


